// 文件管理模块
// 导入数据库操作模块
const db = require("../db/index");
// 用于处理文件
fs = require("fs");

/**
 * file_name        文件名  varchar
 * file_url         文件地址  varchar
 * file_size        文件大小 varchar
 * upload_person    上传者 varchar
 * upload_time      上传时间 varchar
 * download_number  下载次数 int
 */

// 上传文件
exports.uploadFile = (req, res) => {
  // 老名字
  let oldName = req.files[0].filename;
  // 新名字
  let newName = Buffer.from(req.files[0].originalname, "latin1").toString(
    "utf8"
  );
  let upload_time = new Date();
  const sql1 = "select * from files where file_name = ?";
  db.query(sql1, newName, (err, result) => {
    if (result.length > 1) {
      res.send({
        status: 1,
        msg: "文件名已存在",
      });
    } else {
      fs.renameSync("./public/upload/" + oldName, "./public/upload/" + newName);
      const sql = "insert into files set ?";
      db.query(
        sql,
        {
          file_url: `http://127.0.0.1:3007/upload/${newName}`,
          file_name: newName,
          file_size: (req.files[0].size * 1) / 1024,
          upload_time,
          download_number: 0,
        },
        (err, result) => {
          if (err) return res.cc(err);
          res.send({
            status: 0,
            url: "http://127.0.0.1:3007/upload/" + newName,
          });
        }
      );
    }
  });
};

// 绑定上传者
exports.bindFileAndUser = (req, res) => {
  const { name, url } = req.body;
  const sql = "update files set upload_person = ? where file_url = ?";
  db.query(sql, [name, url], (err, result) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      msg: "绑定成功",
    });
  });
};
// 更新下载量
exports.updateDownload = (req, res) => {
  const { download_number, id } = req.body;
  number = download_number * 1 + 1;
  const sql = "update files set  download_number = ? where id = ? ";
  db.query(sql, [number, id], (err, result) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      message: "下载量增加",
    });
  });
};
// 下载文件
exports.downloadFile = (req, res) => {
  const sql = "select * from files where id = ?";
  db.query(sql, req.body.id, (err, result) => {
    if (err) return res.cc(err);
    res.send(result[0].file_url);
  });
};
// 获取文件列表
exports.fileList = (req, res) => {
  const sql = "select * from files ";
  db.query(sql, req.body.id, (err, result) => {
    if (err) return res.cc(err);
    res.send(result);
  });
};
// 获取文件列表总数
exports.fileListLength = (req, res) => {
  const sql = "select * from files ";
  db.query(sql, req.body.id, (err, result) => {
    if (err) return res.cc(err);
    res.send({
      length: result.length,
    });
  });
};
// 搜索文件 模糊搜索
exports.searchFile = (req, res) => {
  const file_name = req.body.file_name;
  const sql = `select * from files where file_name like '%${file_name}%'`;
  db.query(sql, (err, result) => {
    if (err) return res.cc(err);
    res.send(result);
  });
};
// 删除文件 id file_name
exports.deleteFile = (req, res) => {
  const sql = "delete from files where id = ? ";
  db.query(sql, req.body.id, (err, result) => {
    // 使用fsd unlink对文件进行删除
    fs.unlink(`./public/upload/${req.body.file_name}`, (err) => {
      if (err) return res.cc(err);
    });
    if (err) return res.cc(err);
    res.send({
      status: 0,
      message: "删除成功",
    });
  });
};

// 监听换页返回数据 传入 页码 pager  identity
// limit 10 拿到10条数据 offset 跳过多少数据
exports.returnFilesListData = (req, res) => {
  // res.send(req.body.pager)
  // 从?行开始返回共10条数据
  const number = (req.body.pager - 1) * 10;
  const sql = `select * from files  ORDER BY upload_time limit 10 offset ${number}`;
  db.query(sql, (err, result) => {
    if (err) return res.cc(err);
    res.send(result);
  });
};
